﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using WebApplication1;

namespace WebApplication1.Controllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using WebApplication1;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<LotNumber>("LotNumbers");
    builder.EntitySet<ProductDB>("ProductDBs"); 
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class LotNumbersController : ODataController
    {
        private DB db = new DB();

        // GET: odata/LotNumbers
        [EnableQuery]
        public IQueryable<LotNumber> GetLotNumbers()
        {
            return db.LotNumbers;
        }

        // GET: odata/LotNumbers(5)
        [EnableQuery]
        public SingleResult<LotNumber> GetLotNumber([FromODataUri] string key)
        {
            return SingleResult.Create(db.LotNumbers.Where(lotNumber => lotNumber.Lot_LotNumber == key));
        }

        // PUT: odata/LotNumbers(5)
        public async Task<IHttpActionResult> Put([FromODataUri] string key, Delta<LotNumber> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            LotNumber lotNumber = await db.LotNumbers.FindAsync(key);
            if (lotNumber == null)
            {
                return NotFound();
            }

            patch.Put(lotNumber);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LotNumberExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(lotNumber);
        }

        // POST: odata/LotNumbers
        public async Task<IHttpActionResult> Post(LotNumber lotNumber)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.LotNumbers.Add(lotNumber);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (LotNumberExists(lotNumber.Lot_LotNumber))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return Created(lotNumber);
        }

        // PATCH: odata/LotNumbers(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] string key, Delta<LotNumber> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            LotNumber lotNumber = await db.LotNumbers.FindAsync(key);
            if (lotNumber == null)
            {
                return NotFound();
            }

            patch.Patch(lotNumber);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LotNumberExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(lotNumber);
        }

        // DELETE: odata/LotNumbers(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] string key)
        {
            LotNumber lotNumber = await db.LotNumbers.FindAsync(key);
            if (lotNumber == null)
            {
                return NotFound();
            }

            db.LotNumbers.Remove(lotNumber);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        // GET: odata/LotNumbers(5)/ProductDB
        [EnableQuery]
        public SingleResult<ProductDB> GetProductDB([FromODataUri] string key)
        {
            return SingleResult.Create(db.LotNumbers.Where(m => m.Lot_LotNumber == key).Select(m => m.ProductDB));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool LotNumberExists(string key)
        {
            return db.LotNumbers.Count(e => e.Lot_LotNumber == key) > 0;
        }
    }
}
